<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 角色管理渲染 -->
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-row>
            <el-button type="primary" size="small" @click="showrole=true">新增角色</el-button>
          </el-row>
          <el-table
            :data="list"
            border
            style="width: 100%;margin-top: 15px;"
          >
            <el-table-column
              label="序号"
              type="index"
              width="50"
            >
            </el-table-column>
            <el-table-column
              label="名称"
              prop="name"
            >
            </el-table-column>
            <el-table-column
              label="描述"
              prop="description"
            >
            </el-table-column>
            <el-table-column
              label="操作"
            >
              <template v-slot="{row}">
                <el-button size="small" type="success" @click="onShowAsignPerm(row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="onEdit(row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="onDel(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row type="flex" justify="center">
            <el-pagination
              :total="page.total"
              layout="prev,pager,next"
              :page-size="page.pagesize"
              @current-change="onCurrentChange"
            ></el-pagination>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <!-- 绘制表单 -->
          <el-form label-width="88px" style="width:400px; margin:20px 0 0 20px">
            <el-form-item label="公司名称">
              <el-input v-model="info.name" disabled></el-input>
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="info.companyAddress" disabled></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="info.mailbox" disabled></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="info.remarks" disabled></el-input>
            </el-form-item>
          </el-form>

        </el-tab-pane>

      </el-tabs>

    </div>
    <!-- 新增角色 弹框-->
    <el-dialog :visible="showrole" v-bind="$attrs" :title="form.id?'编辑角色':'新增角色'" v-on="$listeners" @open="onOpen" @close="close">
      <el-form ref="formRef" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入角色名称" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入角色描述" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog title="分配权限" width="50%" :visible.sync="showAsignPermDialog">
      <!-- :default-checked-keys="permIds" -->
      <el-tree
        ref="permTreeRef"
        :data="permsList"
        show-checkbox
        :props="{label:'name'}"
        default-expand-all
        check-strictly
        node-key="id"
      ></el-tree>
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="showAsignPermDialog= false">取消</el-button>
        <el-button type="primary" size="small" @click="onAsignPerm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addRole, assignPerm, delRole, getRoleById, getRoleList, getRoles, updateRole } from '@/api/setting'

import { getPermissionList } from '@/api/permisson'
import { translateToTree } from '@/utils'

export default {
  name: 'SettingPage',
  components: {},
  data() {
    return {
      showrole: false,
      info: {},
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      form: {
        name: undefined,
        description: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }],
        description: []
      },
      // 分配权限
      showAsignPermDialog: false,
      // roleIds: [],
      permsList: [],
      currentRoleId: undefined
    }
  },
  created() {
    this.getRoleListFn()
    this.getRolesFn()
    this.getPermissionFn()
  },
  mounted() { },
  methods: {
  /* =============================分配权限 ==============================*/
    async onShowAsignPerm(id) {
      // 显示对话框
      this.showAsignPermDialog = true
      this.currentRoleId = id
      // 选中当前用户角色
      // const { roleIds } = await getEmployeeBaseInfo(id)
      // this.roleIds = roleIds

      // 打开对话框得到对应角色的权限Id数据
      const { permIds } = await getRoleById(id)
      this.$refs.permTreeRef.setCheckedKeys(permIds)
      // this.permIds = permIds
    },
    async onAsignPerm() {
      // 点击确定，获取选中的树节点
      const permIds = this.$refs.permTreeRef.getCheckedKeys()
      console.log(permIds)

      // 发请求
      await assignPerm({
        id: this.currentRoleId,
        permIds
      })
      // await assignRoles({
      // id: this.currentRoleId,
      // roleIds: this.roleIds
      // })
      // 提示成功
      this.$message.success('操作成功')
      // 关闭对话框
      this.showAsignPermDialog = false
    },
    async getPermissionFn() {
      this.permsList = translateToTree(await getPermissionList(), '0')
    },
    /* ==============================⭐  ==============================*/
    // 公司设置=====公司信息
    async getRoleListFn() {
      this.info = await getRoleList(this.$store.state.user.userInfo.companyId)
      console.log(this.info)
    },
    // 公司设置=====角色管理
    async getRolesFn() {
      console.log(this.page)
      const { total, rows } = await getRoles(this.page)
      this.list = rows
      this.page.total = total
    },
    onCurrentChange(val) {
      this.page.page = val
      this.getRolesFn()
    },
    onOpen() {

    },
    async onEdit(id) {
      // 回显数据
      this.form = await getRoleById(id)
      // 打开对话框
      this.showrole = true
    },
    // 公司设置=====删除角色
    onDel(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        // 调用接口
        await delRole(id)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        this.getRolesFn()
      })
    },
    // onClose() {
    // },
    close() {
      this.form = {}
      this.$refs['formRef'].resetFields()// 清空重置
      this.$emit('update:visible', false)
      this.showrole = false
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        // 发请求
        // 三元表达式简写，如果有form.id就编辑，没则新增
        this.form.id
          ? await updateRole(this.form)// 编辑角色
          : await addRole(this.form)// 新增角色
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        this.getRolesFn()
        // 关闭对话框
        this.close()
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
